#include <QDebug>
#include <QFontDatabase>

/* SDK 打点 */
#include <gsettingmonitor.h>
#include "buriedpoint.hpp"

#include "globaldata.h"

GlobalData::GlobalData()
{
    getSystemFontSize();     // 获取字号大小
    getSystemTheme();        // 获取主题色
    getSystemTransparency(); // 获取毛玻璃
    getSystemFontName();     // 获取字体
    changeFontSize();
    changeTheme();
    changeTransparency();
}

GlobalData *GlobalData::getInstance()
{
    static GlobalData globalData;
    return &globalData;
}

GlobalData::~GlobalData()
{
}

void GlobalData::getSystemFontName()
{
    if (QGSettings::isSchemaInstalled(THEME_GSETTINGS)) {
        /* 主题 */
        m_themeGsettings = new QGSettings(THEME_GSETTINGS, QByteArray(), this);
        m_fontName = m_themeGsettings->get(APPLY_FONT_KEY).toString();
        connect(m_themeGsettings, &QGSettings::changed, this, [=]() {
            if (m_themeGsettings->get(APPLY_FONT_KEY).toString() != m_fontName) {
                m_fontName = m_themeGsettings->get(APPLY_FONT_KEY).toString();
                QFontDatabase db;
                if (db.families().contains(m_fontName)) {
                    emit this->sigFontNameChange();
                }
            }
        });
    }
    return;
}

void GlobalData::getSystemTheme()
{
    QString systemTheme = kdk::GsettingMonitor::getSystemTheme().toString();

    qInfo() << "system item : " << systemTheme;
    if (systemTheme == BLACK_THEME) {
        m_systemTheme = BLACK_THEME;
    } else {
        m_systemTheme = LIGHT_THEME;
    }

    return;
}

void GlobalData::getSystemFontSize()
{
    /* 获取系统初始字体 */
    m_fontSize = kdk::GsettingMonitor::getSystemFontSize().toDouble();
    qInfo() << "system systemFontSize : " << m_fontSize;

    //默认大小12px,换算成pt为9
    double baseFontSize = 9;
    double nowFontSize = baseFontSize * (m_fontSize / 11) / 12; // 11为系统默认大小，魔鬼数字，自行处理

    m_font12pxToPt = nowFontSize * 12;
    m_font14pxToPt = nowFontSize * 14;
    m_font16pxToPt = nowFontSize * 16;
    m_font18pxToPt = nowFontSize * 18;

    return;
}

void GlobalData::getSystemTransparency()
{
    double systemTransparency = kdk::GsettingMonitor::getSystemTransparency().toDouble();

    qInfo() << "system systemTransparency : " << systemTransparency;
    m_transparency = systemTransparency * 255;

    return;
}

void GlobalData::changeFontSize()
{
    this->connect(kdk::GsettingMonitor::getInstance(), &kdk::GsettingMonitor::systemFontSizeChange, this, [=]() {
        getSystemFontSize();
        emit this->sigFontSizeChange();
    });
    return;
}

void GlobalData::changeTheme()
{
    this->connect(kdk::GsettingMonitor::getInstance(), &kdk::GsettingMonitor::systemThemeChange, this, [=]() {
        getSystemTheme();
        emit this->sigTheme();
    });
    return;
}

void GlobalData::changeTransparency()
{
    this->connect(kdk::GsettingMonitor::getInstance(), &kdk::GsettingMonitor::systemTransparencyChange, this, [=]() {
        getSystemTransparency();
        emit this->sigTransparency();
    });
    return;
}

QFont GlobalData::getFontSize12px()
{
    QFont font;
    font.setPointSizeF(m_font12pxToPt);
    font.setFamily(m_fontName);

    return font;
}

QFont GlobalData::getFontSize14px()
{
    QFont font;
    font.setPointSizeF(m_font14pxToPt);
    font.setFamily(m_fontName);

    return font;
}

QFont GlobalData::getFontSize16px()
{
    QFont font;
    font.setPointSizeF(m_font16pxToPt);
    font.setFamily(m_fontName);

    return font;
}

QFont GlobalData::getFontSize18px()
{
    QFont font;
    font.setPointSizeF(m_font18pxToPt);
    font.setFamily(m_fontName);

    return font;
}

QString GlobalData::getThemeData()
{
    return m_systemTheme;
}

double GlobalData::getTransparencyData()
{
    return m_transparency;
}

QString GlobalData::getFontNameData()
{
    return m_fontName;
}

void GlobalData::SDKPointFileUpload()
{
    ::kabase::BuriedPoint buriedPointTest;

    if (buriedPointTest.functionBuriedPoint(::kabase::AppName::KylinServiceSupport , ::kabase::BuriedPoint::PT::KylinServiceSupportFileUpload)) {
        qCritical() << "Error : buried point fail !";
    };

    return;
}

void GlobalData::SDKPointFileCopy()
{
    ::kabase::BuriedPoint buriedPointTest;

    if (buriedPointTest.functionBuriedPoint(::kabase::AppName::KylinServiceSupport , ::kabase::BuriedPoint::PT::KylinServiceSupportFileCopy)) {
        qCritical() << "Error : buried point fail !";
    };

    return;
}

void GlobalData::SDKPointJumpOfficicalWebsite()
{
    ::kabase::BuriedPoint buriedPointTest;

    if (buriedPointTest.functionBuriedPoint(::kabase::AppName::KylinServiceSupport , ::kabase::BuriedPoint::PT::KylinServiceSupportJumpOfficicalWebsite)) {
        qCritical() << "Error : buried point fail !";
    };

    return;
}

void GlobalData::SDKPointJumpOnlineService()
{
    ::kabase::BuriedPoint buriedPointTest;

    if (buriedPointTest.functionBuriedPoint(::kabase::AppName::KylinServiceSupport , ::kabase::BuriedPoint::PT::KylinServiceSupportJumpOnlineService)) {
        qCritical() << "Error : buried point fail !";
    };

    return;
}